package com.hfh.server.netty_server;

import com.google.common.collect.Lists;
import com.hfh.server.common.Constants;
import com.hfh.server.netty_server.service.NettyServerMessageImpl;
import com.hfh.server.netty_server.service.NettyServerMessageService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liuxinghong
 * @Description:  服务端发送测试类
 * @date 2019/9/4 000414:50
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class NettyServerTestController {

    @Autowired
    private NettyServerMessageService nettyServerMessageService;

    @GetMapping("/sendByKey")
    public  void sendMessage(@Param("key") String key){
        log.info("开始发送........");
        MessageDTO message = MessageDTO.builder().type(NettyConfig.NETTY_COMMON_TYPE)
                .messageBody("发送业务消息到客户端")
                .senderId(NettyConfig.NETTY_SERVER_ID)
                .sendTime(DateTime.now().toString(Constants.YYYY_MM_DD_HH_MM_SS))
                .receiveIds(key).build();
        nettyServerMessageService.sendClientMessage(Lists.newArrayList(key),message);
        log.info("服务端发送消息到单一客户端，客户端key"+message.getReceiveIds());
    }
    @GetMapping("/sendAllClient")
    public  void sendAll(){
        MessageDTO message = MessageDTO.builder().type(NettyConfig.NETTY_COMMON_TYPE)
                .messageBody("发送业务消息到所有的客户端")
                .senderId(NettyConfig.NETTY_SERVER_ID)
                .sendTime(DateTime.now().toString(Constants.YYYY_MM_DD_HH_MM_SS))
                .receiveIds("").build();
        nettyServerMessageService.sendAllClient(message);
      log.info("发送业务消息到所有的客户端完成");
    }

}
